package ro.pub.acs.android;

import java.util.Vector;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.RectF;
import android.graphics.Paint.Style;
import android.graphics.drawable.Drawable;
import android.os.Bundle;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
import com.google.android.maps.Projection;

/**
 * In acest tutorial vom invata sa folosim MapActivity si sa plasam pini customizabili pe harta.
 * 
 * http://developer.android.com/resources/tutorials/views/hello-mapview.html
 * http://code.google.com/android/add-ons/google-apis/mapkey.html
 * 
 * @author Vlad
 *
 */

//de data aceasta vom extinde MapActivity, si nu simplu Activity
public class MapsAdvanced extends MapActivity {

	//referinta catre view-ul de ti harta
	private MapView mapView;

	//overlay-ul (strat, layer) care afiseaza locatia mea pe harta
	private MyLocationOverlay myLocOverlay;

	//overlay-ul care afiseaza pini customizati de mine pe harta
	private CustomItemizedOverlay overlayWithItems;

	//overlay-ul in care vor fi afisate descrierile pinilor in momentul in care sunt click-ati
	private CustomOverlay overlay;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		mapView = (MapView) findViewById(R.id.mapview); //preluam view-ul hartii
		mapView.setKeepScreenOn(true); //impiedicam stingerea ecranului
		mapView.setBuiltInZoomControls(true); //permitem controalele de zoom default
		mapView.getController().setZoom(12); //setam un zoom corespunzator

		myLocOverlay = new MyLocationOverlay(this, mapView); //generam un overlay pentru ocatia noastra
		myLocOverlay.enableMyLocation(); //dorim afisarea unei buline albastre pe harta, in locul in care ne aflam
		myLocOverlay.enableCompass(); //dorim afisarea busolei in coltul din stanga sus
		
		//in momentul in care imi este stabilita pozitia, harta isi muta focusul pe locatia mea
		myLocOverlay.runOnFirstFix(new Runnable() {  
			public void run() {
				mapView.getController().animateTo(myLocOverlay.getMyLocation());
			}
		});
		
		//adaugam myLocOverlay pe view-ul hartii pentru a fi vizibil pentru utilizator
		mapView.getOverlays().add(myLocOverlay);

		//generam un nou overlay, de data aceasta este vorba de unul in care vom avea pini customizati complet de catre noi
		overlayWithItems = new CustomItemizedOverlay(this.getResources().getDrawable(R.drawable.marker));
		
		GeoPoint geoPoint = new GeoPoint(
				(int)(44.4351282 * 1E6),  //latitudine
				(int)(26.0532188 * 1E6)); //longitutide
		OverlayItem overlayItem = new OverlayItem(geoPoint, "Pe langa Politehnica", ""); //cream itemul (geolocatie + descriere)
		overlayWithItems.addOverlay(overlayItem); //adaugam itemul pe harta
		
		geoPoint = new GeoPoint(
				(int)(44.4383839 * 1E6),  //latitudine
				(int)(26.0515022 * 1E6)); //longitutide
		overlayItem = new OverlayItem(geoPoint, "Rectorat", ""); //cream itemul (geolocatie + descriere)
		overlayWithItems.addOverlay(overlayItem); //adaugam itemul pe harta
		
		geoPoint = new GeoPoint(
				(int)(44.4456839 * 1E6),  //latitudine
				(int)(26.0517061 * 1E6)); //longitutide
		overlayItem = new OverlayItem(geoPoint, "Cantina R3", ""); //cream itemul (geolocatie + descriere)
		overlayWithItems.addOverlay(overlayItem); //adaugam itemul pe harta

		mapView.getOverlays().add(overlayWithItems);

		//generam un ultim overlay, cel in care vor fi afisate descrierile pinilor in momentul in care sunt click-ati
		overlay = new CustomOverlay();
		mapView.getOverlays().add(overlay);

		mapView.postInvalidate();

	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}

	
	//clasa care defineste overlay-ul care afiseaza pini customizati de mine pe harta
	@SuppressWarnings("unchecked")
	private class CustomItemizedOverlay extends ItemizedOverlay {

		//Asta va fi lista de pined items on the map
		private Vector<OverlayItem> myOverlayItems = new Vector<OverlayItem>();

		public CustomItemizedOverlay(Drawable defaultMarker) {
			super(boundCenterBottom(defaultMarker));
			populate(); //this method prepares the items to be drawn
		}

		@Override
		protected OverlayItem createItem(int i) {
			return myOverlayItems.get(i);
		}

		@Override
		public int size() {
			return myOverlayItems.size();
		}

		/**
		 * This method adds more overlay items to our current items list.
		 * @param overlay
		 */
		public void addOverlay(OverlayItem overlay) {
			myOverlayItems.add(overlay);
			populate(); //this method prepares the items to be drawn
		}

		@Override
		protected boolean onTap(int i) {
			overlay.setPoint(myOverlayItems.get(i).getPoint());
			overlay.setText(myOverlayItems.get(i).getTitle());
			return(true);
		}

	}

	//clasa care defineste overlay-ul in care vor fi afisate descrierile pinilor in momentul in care sunt click-ati
	private class CustomOverlay extends Overlay {

		private GeoPoint geopoint = null;
		private String textToDisplay = null;

		public void setPoint (GeoPoint geoPoint) {
			this.geopoint = geoPoint;
		}

		public void setText(String textToDisplay) {
			this.textToDisplay = textToDisplay;
		}

		@Override
		public void draw(Canvas canvas, MapView mapView, boolean shadow) {
			if ((geopoint == null) || (textToDisplay == null)) {
				return;
			}

			// Transfrom geoposition to Point on canvas
			Projection projection = mapView.getProjection();
			Point point = new Point();
			projection.toPixels(geopoint, point);

			Paint p = new Paint();
			int sz = 5;

			// Draw point caption and its bounding rectangle
			p.setTextSize(14);
			p.setAntiAlias(true);
			int sw = (int)(p.measureText(textToDisplay) + 0.5f);
			int sh = 25;
			int sx = point.x - sw / 2 - 5;
			int sy = point.y - sh - sz - 30;
			RectF rec = new RectF(sx, sy, sx + sw + 10, sy + sh);

			p.setStyle(Style.FILL);
			p.setARGB(128, 0, 0, 0);
			canvas.drawRoundRect(rec, 5, 5, p);

			//conturul alb
			p.setStyle(Style.STROKE);
			p.setARGB(255, 255, 255, 255);
			canvas.drawRoundRect(rec, 5, 5, p);

			canvas.drawText(textToDisplay, sx + 5, sy + sh - 8, p);
		}
	}

}